package com.yjhb.android.sbz.activity;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.SubjectAdapter;
import com.yjhb.android.sbz.entity.Subject;
import com.yjhb.android.sbz.mvp.subject.SubjectPresenter;
import com.yjhb.android.sbz.mvp.subject.SubjectView;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/30 14:15
 */
public class SubjectActivity extends BaseActivity<SubjectPresenter> implements SubjectView {
    private final String TAG = "SubjectActivity";
    @BindView(R.id.emptyView)
    EmptyView emptyView;
    @BindView(R.id.back)
    FrameLayout back;
    @BindView(R.id.subject_title_tv)
    TextView subjectTitleTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.answer_tv)
    TextView answerTv;
    @BindView(R.id.title_tv)
    TextView title_tv;

    int currIndex;
    boolean isErr;
    List<Subject> subjects = new ArrayList<>();
    SubjectAdapter adapter;


    @Override
    protected SubjectPresenter initPresenter() {
        return new SubjectPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_subject;
    }

    @Override
    protected void initUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        adapter = new SubjectAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        currIndex = 0;
        presenter.getSubjects();
    }

    @Override
    public void refresh() {
        super.refresh();
        if (subjects != null && subjects.size() > currIndex) {
            Subject subject = subjects.get(currIndex);
            title_tv.setText((currIndex + 1) + "/" + subjects.size());
            subjectTitleTv.setText(subject.getQuestionContent());
            if (isErr) {
                char i = (char) (65 + subject.getRightOption() - 1);
                answerTv.setText("正确答案：" + i);
                answerTv.setVisibility(View.VISIBLE);
            } else {
                answerTv.setVisibility(View.GONE);
            }
            List<String> strings = new ArrayList<>();
            strings.add(subject.getOptionOne());
            strings.add(subject.getOptionTwo());
            adapter.setData(strings);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void regEvent() {
        emptyView.setOnReloadClickListener(new EmptyView.OnReloadClickListener() {
            @Override
            public void onReloadClick(EmptyView emptyView) {
                initData();
            }
        });
    }

    @OnClick(R.id.submit_tv)
    void submit() {
        int selectIndex = adapter.getSelectIndex();
        if (selectIndex == -1) {
            ZxToast.showINFOBottomToast("请先选择哦");
            return;
        }
        if (currIndex < subjects.size()) {
            int answer = subjects.get(currIndex).getRightOption() - 1;
            if (selectIndex == answer) {
                isErr = false;
                currIndex++;
                if (currIndex >= subjects.size()) {
                    setResult(RESULT_OK);
                    finish();
                }
            } else {
                isErr = true;
            }
        }
        refresh();
    }

    @Override
    public void onWait() {
        emptyView.setVisibility(View.VISIBLE);
        emptyView.showLoading();
    }

    @Override
    public void onSuccess(List<Subject> data) {
        emptyView.setVisibility(View.GONE);
        if (data != null) {
            this.subjects.clear();
            this.subjects.addAll(data);
        }
        refresh();
    }

    @Override
    public void onFailure(int code, String msg) {
        emptyView.showFail("加载失败", true);
    }

}
